package ${dir}.dao.impl;

import java.util.List;
import java.util.ArrayList;
import ${dir}.dao.${table.name!}Dao;
import ${dir}.entity.${table.name!};
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import ${dir}.util.BaseDao;
/**
 * @author gc
 */
public class ${table.name!}DaoImpl extends BaseDao implements ${table.name!}Dao {


	@Override
	public int add(${table.name!} obj) {
		String sql = "insert into ${table.name!}(<#list table.colums as c>${c.cname}<#if c_index<table.colums?size-1>,</#if></#list>) values(<#list table.colums as c>?<#if c_index<table.colums?size-1>,</#if></#list>)";
		return super.executeUpdate(sql,<#list table.colums as c>obj.get${c.bigName}()<#if c_index<table.colums?size-1>,</#if></#list>);
	}
	@Override
	public ${table.name!} selectOne(Serializable id) throws SQLException {
		String sql = "select * from ${table.name!} where ${table.pkColumns.cname} = ?";
		${table.name!} entity = null;
		ResultSet rs = super.executeQuery(sql,id);
		if(rs.next()){
			entity = new ${table.name!}();
			<#list table.colums as c>
			<#if c.type=='Integer'>
			entity.set${c.bigName}(rs.getInt("${c.cname}"));
			<#else >
			entity.set${c.bigName}(rs.get${c.type}("${c.cname}"));
			</#if>
			</#list>
		}
		return entity;
	}
	@Override
	public int update(${table.name!} obj){
		String sql = "update ${table.name!} set <#list table.colums as c><#if c.status!=1>${c.cname}=?</#if><#if c_index<table.colums?size-1>,</#if></#list> where where ${table.pkColumns.cname} = ?";
		return super.executeUpdate(sql,<#list table.colums as c><#if c.status!=1>obj.get${c.bigName}(),</#if></#list>obj.get${table.pkColumns.bigName}());
	}
	@Override
	public int delete(Serializable id){
		String sql = "delete from ${table.name!} where ${table.pkColumns.cname} = ?";
		return super.executeUpdate(sql,id);
	}
	public List<${table.name!}> selectAll() throws SQLException{
		String sql = "select * from ${table.name!}";
		List<${table.name!}> list = new ArrayList<>();
		ResultSet rs = super.executeQuery(sql);
		while(rs.next()){
			${table.name!} entity = new ${table.name!}();
			<#list table.colums as c>
				<#if c.type=='Integer'>
			entity.set${c.bigName}(rs.getInt("${c.cname}"));
				<#else >
			entity.set${c.bigName}(rs.get${c.type}("${c.cname}"));
				</#if>
			</#list>
			list.add(entity);
		}
		return list;
	}

	@Override
	public List<${table.name!}> selectByPage(int pageNum, int pageSize) throws SQLException {
		<#if db==1>
		int begin = (pageNum-1)*pageSize;
		int end = pageSize;
		String sql = "select * from ${table.name!} limit ?,?";
		<#else>
		int begin = (pageNum-1)*pageSize;
		int end = pageNum*pageSize;
		String sql = "select * from(select t.*,rownum r from ${table.name!} t) where r>? and r<=?";
		</#if>
		List<${table.name!}> list = new ArrayList<>();
		ResultSet rs = super.executeQuery(sql,begin,end);
		while(rs.next()){
			${table.name!} entity = new ${table.name!}();
				<#list table.colums as c>
					<#if c.type=='Integer'>
			entity.set${c.bigName}(rs.getInt("${c.cname}"));
					<#else >
			entity.set${c.bigName}(rs.get${c.type}("${c.cname}"));
					</#if>
				</#list>
			list.add(entity);
		}
		return list;
    }
}
